function largestRectangleArea(heights) {
    const length = heights.length;
    const leftHeightDp = [],
        rightHeightDp = [];
    // 记录每个柱子 左边第一个小于该柱子的下标
    leftHeightDp[0] = -1; // 注意这里初始化，防止下面while死循环
    rightHeightDp[length - 1] = length; // 注意这里初始化，防止下面while死循环
    for (let i = 1; i < length; i++) {
        let j = i - 1;
        // 这里不是用if，而是不断向左寻找的过程
        while (j >= 0 && heights[i] <= heights[j]) {
            j = leftHeightDp[j];
        }
        leftHeightDp[i] = j;
    }
    // 记录每个柱子 右边第一个小于该柱子的下标
    for (let i = length - 2; i >= 0; i--) {
        let j = i + 1;
        // 这里不是用if，而是不断向右寻找的过程
        while (j < length && heights[i] <= heights[j]) {
            j = rightHeightDp[j];
        }
        rightHeightDp[i] = j;
    }
    // // 求和
    let resMax = 0;
    for (let i = 0; i < length; i++) {
        let area = heights[i] * (rightHeightDp[i] - leftHeightDp[i] - 1);
        resMax = Math.max(resMax, area);
    }
    return resMax;
};
console.log(largestRectangleArea([5,6,4]))
